#!/usr/bin/perl

## This file is part of the Simplemail project.
#  Copyright (c) 2008 the Simplemail Team (see authors).
# 
#  For the full copyright and license information, please view the COPYING
#  file that was distributed with this source code. You can also view the
#  COPYING file online at http://unixforum.org.ua/misc/COPYING.txt
# 
#  @copyright  (c) the authors
#  @author     Dmitriy Shilenko <rtyug@ukr.net>
#  @license    GPLv2 http://unixforum.org.ua/misc/COPYING.txt
#  @package    Panel Mailbox
#  @version    $Id: $

use CGI::Carp qw(fatalsToBrowser);    # used only for tests
use strict;
use CGI;
#use Encode qw(encode_utf8);
use HTML::Template;


# use FindBin::Real;
# use lib FindBin::Real::Bin() . '/lib';


use FindBin qw/$Bin/;
use lib "$Bin/lib";

#use lib "/home/mydm/www/simplemail/lib";


our $se;
require "lib/config.pl";

my $query = new CGI;

my $action    = $query->param("action");
my $entrance2 = $query->param("entrance2");

my $p;
%{$p} = $query->Vars;

my $postfixform = $query->param("postfixform");

use session;

my $to_obj = session->new( se => $se, query => $query, p => $p );


$to_obj->fetch_error();


if ( $to_obj->fetch_error() == 0 ) {
    print $query->header( -charset => "utf8" );
    print
'The maximal amount of errors at an entrance is exhausted, make attempt after a while ';
    exit;
}

my $session = $to_obj->select_session();

if (  !$session
    || $session < 1
    || ( $session->{user} == 4 ) && ( !defined($entrance2) ) )
{

    $to_obj->entrance();
    exit;
}

elsif ( !$session
    || $session < 1
    || ( $session->{user} == 4 ) && ( defined($entrance2) ) )
{

    $to_obj->entrance2();
    exit;
}

elsif ( $session->{user} && $session->{user} > 4 ) {

    if (   $action eq 'postfix'
        || defined($postfixform)
        || $query->param("fDomain")
        || $query->param("fAdmin")
        || $query->param("username_domain") )
    {
        postfix();
    }
    elsif ( $action eq 'logout' ) {

        $to_obj->exit2();
       # exit;

    }
    else {    
    
        if ( !$session->{user} ) {

            $to_obj->entrance();
            exit;
        }
        else {
            postfix();

        }

    }
}

else {

    $to_obj->entrance();
    exit;
}

sub postfix {

    my $domain = $query->param("fDomain");
    my $pa     = $query->param("postfixactive");

    if ( $pa eq 'editemailactive' ) {

        require delete2;
        my $to_obj = delete2->new(
            se      => $se,
            query   => $query,
            session => $session,
            p       => $p
        );
        $to_obj->edite_mail_active();
    }

    elsif ( $pa eq 'editedomainactive' ) {

        require delete2;

        my $to_obj = delete2->new(
            se      => $se,
            query   => $query,
            session => $session,
            p       => $p
        );

        $to_obj->edit_domain_active();

    }

    elsif ( $pa eq 'editeadminactive' && $session->{name} eq 'Admin' ) {

        require delete2;
        my $to_obj = delete2->new(
            se      => $se,
            query   => $query,
            session => $session,
            p       => $p
        );
        $to_obj->edit_admin_active();
    }

    elsif ( $pa eq 'deletedomain' ) {
        require delete2;
        my $to_obj = delete2->new(
            se      => $se,
            query   => $query,
            session => $session,
            p       => $p
        );
        $to_obj->delete_domain();
    }

    elsif ( $pa eq 'deletemailbox' ) {
        require delete2;
        my $to_obj = delete2->new(
            se      => $se,
            query   => $query,
            session => $session,
            p       => $p
        );
        $to_obj->delete_mailbox();

    }

    elsif ( $pa eq 'deleteadmin' && $session->{'name'} eq 'Admin' ) {
        require delete2;
        my $to_obj = delete2->new(
            se      => $se,
            query   => $query,
            session => $session,
            p       => $p
        );
        $to_obj->delete_admin();
    }

    elsif ( $pa eq 'deletealias' ) {
        require delete2;
        my $to_obj = delete2->new(
            se      => $se,
            query   => $query,
            session => $session,
            p       => $p
        );
        $to_obj->delete_alias();
    }

    elsif ( $pa eq 'listdomain' || ( $query->param("username_domain") ) ) {
        require listdomain;
        my $to_obj = listdomain->new(
            se      => $se,
            query   => $query,
            session => $session,
            p       => $p
        );
        $to_obj->total_list_domain();
        $to_obj->page_list_domain();
        $to_obj->return_list_domain();
        $to_obj->templ_hash_domain_list();
    }

    elsif ( $pa eq 'createdomain' ) {

        require createdomain;
        my $to_obj =
          createdomain->new( se => $se, query => $query, session => $session );
        $to_obj->return_create_domain();
        $to_obj->templ_hash_domain();
    }

    elsif ( $pa eq 'editdomain' ) {
        require editdomain;
        my $to_obj = editdomain->new(
            se      => $se,
            query   => $query,
            session => $session,
            p       => $p
        );
        $to_obj->return_edit_domain();
        $to_obj->templ_hash_domain();
    }

    elsif ( $postfixform eq 'createdomain2' ) {

        require createdomain2;
        my $to_obj = createdomain2->new(
            se      => $se,
            query   => $query,
            session => $session,
            p       => $p
        );
        $to_obj->valid_param();
        $to_obj->valid_param2();
        $to_obj->insert_domain();
        $to_obj->return_create_domain();
        $to_obj->templ_hash_domain();
    }

    elsif ( $postfixform eq 'editdomain2' ) {
        require editdomain2;
        my $to_obj = editdomain2->new(
            se      => $se,
            query   => $query,
            session => $session,
            p       => $p
        );
        $to_obj->valid_param();
        $to_obj->valid_param2_edit();
        $to_obj->edit_domain();
        $to_obj->return_edit_domain();
        $to_obj->templ_hash_domain();
    }

    elsif ( $pa eq 'createalias' ) {
        require createalias;
        my $to_obj =
          createalias->new( se => $se, query => $query, session => $session );
        $to_obj->return_create_alias();
        $to_obj->templ_hash_create_alias();
    }

    elsif ( $postfixform eq 'createalias2' ) {
        require createalias;
        my $to_obj = createalias->new(
            se      => $se,
            query   => $query,
            session => $session,
            p       => $p
        );
        $to_obj->return_create_alias2();
        $to_obj->return_create_alias();
        $to_obj->templ_hash_create_alias();
    }

    elsif ( $pa eq 'editalias' ) {
        require editalias;
        my $to_obj = editalias->new(
            se      => $se,
            query   => $query,
            session => $session,
            p       => $p
        );
        $to_obj->return_edit_alias();
        $to_obj->templ_hash_edit_alias();
    }

    elsif ( $postfixform eq 'editalias2' ) {
        require editalias;
        my $to_obj = editalias->new(
            se      => $se,
            query   => $query,
            session => $session,
            p       => $p
        );
        $to_obj->return_edit_alias2();
        $to_obj->return_edit_alias();
        $to_obj->templ_hash_edit_alias();

    }

    elsif ( $pa eq 'createmailbox' ) {
        require createmailbox;
        my $to_obj =
          createmailbox->new( se => $se, query => $query, session => $session );
        $to_obj->return_create_mailbox();
        $to_obj->templ_hash_mailbox();
    }

    elsif ( $postfixform eq 'createmailbox2' ) {
        require createmailbox2;
        my $to_obj = createmailbox2->new(
            se      => $se,
            query   => $query,
            session => $session,
            p       => $p
        );
        $to_obj->valid_param_mailbox();
        $to_obj->valid_param2_mailbox();
        $to_obj->insert_mailbox();
        $to_obj->return_create_mailbox();
        $to_obj->templ_hash_mailbox();
    }

    elsif ( $pa eq 'editmailbox' && defined( $query->param("mailbox") ) ) {
        require editmailbox;
        my $to_obj = editmailbox->new(
            se      => $se,
            query   => $query,
            session => $session,
            p       => $p
        );
        $to_obj->return_edit_mailbox();
        $to_obj->templ_hash_mailbox();
    }

    elsif ( $postfixform eq 'editmailbox2' ) {
        require editmailbox2;
        my $to_obj = editmailbox2->new(
            se      => $se,
            query   => $query,
            session => $session,
            p       => $p
        );
        $to_obj->valid_param_mailbox();
        $to_obj->valid_param2_mailbox_edit();
        $to_obj->edit_mailbox();
        $to_obj->return_edit_mailbox();
        $to_obj->templ_hash_mailbox();
    }

    elsif ( $pa eq 'listvirtual' || ( defined( $query->param("fDomain") ) ) ) {
        require listmailbox;
        my $to_obj = listmailbox->new(
            se      => $se,
            query   => $query,
            session => $session,
            p       => $p
        );
        $to_obj->total_list_mailbox();
        $to_obj->page_list_mailbox();
        $to_obj->return_list_mailbox();
        $to_obj->templ_hash_mailbox_list();
    }

    elsif ( $pa eq 'listadmin' && $session->{name} eq 'Admin' ) {
        require listadmin;
        my $to_obj = listadmin->new(
            se      => $se,
            query   => $query,
            session => $session,
            p       => $p
        );
        $to_obj->total_list_admin();
        $to_obj->page_list_admin();
        $to_obj->return_list_admin();
        $to_obj->templ_hash_admin_list();
    }

    elsif ( $pa eq 'createadmin' && $session->{name} eq 'Admin' ) {
        require createadmin;
        my $to_obj =
          createadmin->new( se => $se, query => $query, session => $session );
        $to_obj->return_create_admin();
        $to_obj->templ_hash_admin();
    }

    elsif ( $postfixform eq 'createadmin2' && $session->{name} eq 'Admin' ) {
        require createadmin2;
        my $to_obj = createadmin2->new(
            se      => $se,
            query   => $query,
            session => $session,
            p       => $p
        );
        $to_obj->valid_param_createadmin();
        $to_obj->valid_param_createadmin2();
        $to_obj->insert_admin();
        $to_obj->return_create_admin();
        $to_obj->templ_hash_admin();
    }

    elsif ( $pa eq 'editadmin' && $session->{'name'} eq 'Admin' ) {
        require editadmin;
        my $to_obj = editadmin->new(
            se      => $se,
            query   => $query,
            session => $session,
            p       => $p
        );
        $to_obj->return_edit_admin();
        $to_obj->templ_hash_admin();
    }

    elsif ( $postfixform eq 'editadmin2' && $session->{'name'} eq 'Admin' ) {
        require editadmin2;
        my $to_obj = editadmin2->new(
            se      => $se,
            query   => $query,
            session => $session,
            p       => $p
        );
        $to_obj->valid_param_editadmin();
        $to_obj->valid_param_editadmin2();
        $to_obj->edit_admin();
        $to_obj->return_edit_admin2();
        $to_obj->templ_hash_admin();
    }

    elsif ( ( $pa eq 'logms' || $query->param("fAdmin") )
        && $session->{'name'} eq 'Admin' )
    {
        require listlogms;
        my $to_obj = listlogms->new(
            se      => $se,
            query   => $query,
            session => $session,
            p       => $p
        );
        $to_obj->total_list_logms();
        $to_obj->page_list_logms();
        $to_obj->return_list_logms();
        $to_obj->templ_hash_logms_list();
    }

    elsif ( $pa eq 'log' && $session->{'name'} eq 'Admin' ) {
        require listlogmta;
        my $to_obj = listlogmta->new(
            se      => $se,
            query   => $query,
            session => $session,
            p       => $p
        );
        $to_obj->total_list_logmta();
        $to_obj->page_list_logmta();
        $to_obj->return_list_logmta();
        $to_obj->templ_hash_logmta_list();
    }

    elsif ( ( $postfixform eq 'searchlog' || $pa eq 'logsearch' )
        && $session->{name} eq 'Admin' )
    {
        require listlogmta;
        my $to_obj = listlogmta->new(
            se      => $se,
            query   => $query,
            session => $session,
            p       => $p
        );
        $to_obj->total_list_logmta_search();
        $to_obj->page_list_logmta_search();
        $to_obj->return_list_logmta_search();
        $to_obj->templ_hash_logmta_list();
    }

    else {
        require posfixform;
        my $to_obj = posfixform->new(
            se      => $se,
            query   => $query,
            session => $session,
            p       => $p
        );
        $to_obj->select_all_posfixform();
        $to_obj->templ_hash_posfixform();
    }

}

